## install.packages("Matching")
## install.packages("rgenoud")

library(Matching)
library(rgenoud)

matching <- read.csv("~/Dropbox/switchers/Matching/data/contribDB.csv")
matching <- transform(matching, id = as.numeric(factor(bonica_rid)))
attach(matching)

## Setting the covariates to (1) match on; and (2) obtain balance on:

covariates_match <- cbind(switcher_code, post_dem, dwnom1, seniority, dpres, les, votepct, pre_in_district, pre_out_district, pre_unknown, general_length)

covariates_balance <- cbind(switcher_code, post_dem, dwnom1, seniority, dpres, les, votepct, pre_in_district, pre_out_district, pre_unknown, general_length)

## Performing the match -- 1-to-1 WITH replacement:

	## Finding balance -- 
match_1 <- GenMatch(Tr = switcher, X = covariates_match, BalanceMatrix = covariates_balance, M = 1, pop.size = 20000, max.generations = 10, wait.generations = 5, hard.generation.limit = FALSE, int.seed = 693, unif.seed = 560, fit.func = "pvals", exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0)), replace = TRUE) 

	## Estimating the causal effect using the optimal weights 
in_district_1 <- Match(Y = post_in_district, Tr = switcher, X = covariates_match, M = 1, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_1)
summary(in_district_1)

out_district_1 <- Match(Y = post_out_district, Tr = switcher, X = covariates_match, M = 1, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_1, match.out = in_district_1)
summary(out_district_1)

unknown_district_1 <- Match(Y = post_unknown, Tr = switcher, X = covariates_match, M = 1, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_1, match.out = in_district_1)
summary(unknown_district_1)

	## Checking balance 
balance_1 <- MatchBalance(switcher ~ switcher_code + post_dem + dwnom1 + seniority + dpres + les + votepct + pre_in_district + pre_out_district + pre_unknown + general_length, match.out = in_district_1, nboots = 5000)

	## Generating the matched dataset --
treated_1 <- matching[in_district_1$index.treated,]
treated_1$pair_id <- treated_1$id
control_1 <- matching[in_district_1$index.control,]
control_1$pair_id <- treated_1$id

matched_geog_1 <- rbind(treated_1, control_1)
matched_geog_1 <- matched_geog_1[order(matched_geog_1$pair_id),]
matched_geog_1$match <- 1

## Performing the match -- 1-to-2 WITH replacement:

	## Finding balance -- 
match_2 <- GenMatch(Tr = switcher, X = covariates_match, BalanceMatrix = covariates_balance, M = 2, pop.size = 20000, max.generations = 10, wait.generations = 5, hard.generation.limit = FALSE, int.seed = 771, unif.seed = 960, fit.func = "pvals", exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0)), replace = TRUE) 

	## Estimating the causal effect using the optimal weights 
in_district_2 <- Match(Y = post_in_district, Tr = switcher, X = covariates_match, M = 2, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_2)
summary(in_district_2)

out_district_2 <- Match(Y = post_out_district, Tr = switcher, X = covariates_match, M = 2, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_2, match.out = in_district_2)
summary(out_district_2)

unknown_district_2 <- Match(Y = post_unknown, Tr = switcher, X = covariates_match, M = 2, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_2, match.out = in_district_2)
summary(unknown_district_2)

	## Checking balance 
balance_2 <- MatchBalance(switcher ~ switcher_code + post_dem + dwnom1 + seniority + dpres + les + votepct + pre_in_district + pre_out_district + pre_unknown + general_length, match.out = in_district_2, nboots = 5000)

	## Generating the matched dataset --
treated_2 <- matching[in_district_2$index.treated,]
treated_2$pair_id <- treated_2$id
control_2 <- matching[in_district_2$index.control,]
control_2$pair_id <- treated_2$id

matched_geog_2 <- rbind(treated_2, control_2)
matched_geog_2 <- matched_geog_2[order(matched_geog_2$pair_id),]
matched_geog_2 <- matched_geog_2[!duplicated(matched_geog_2), ]
matched_geog_2$match <- 2

## Performing the match -- 1-to-3 WITH replacement:

	## Finding balance -- 
match_3 <- GenMatch(Tr = switcher, X = covariates_match, BalanceMatrix = covariates_balance, M = 3, pop.size = 20000, max.generations = 10, wait.generations = 5, hard.generation.limit = FALSE, int.seed = 564, unif.seed = 117, fit.func = "pvals", exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0)), replace = TRUE) 

	## Estimating the causal effect using the optimal weights 
in_district_3 <- Match(Y = post_in_district, Tr = switcher, X = covariates_match, M = 3, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_3)
summary(in_district_3)

out_district_3 <- Match(Y = post_out_district, Tr = switcher, X = covariates_match, M = 3, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_3, match.out = in_district_3)
summary(out_district_3)

unknown_district_3 <- Match(Y = post_unknown, Tr = switcher, X = covariates_match, M = 3, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_3, match.out = in_district_3)
summary(unknown_district_3)

	## Checking balance 
balance_3 <- MatchBalance(switcher ~ switcher_code + post_dem + dwnom1 + seniority + dpres + les + votepct + pre_in_district + pre_out_district + pre_unknown + general_length, match.out = in_district_3, nboots = 5000)

	## Generating the matched dataset --
treated_3 <- matching[in_district_3$index.treated,]
treated_3$pair_id <- treated_3$id
control_3 <- matching[in_district_3$index.control,]
control_3$pair_id <- treated_3$id

matched_geog_3 <- rbind(treated_3, control_3)
matched_geog_3 <- matched_geog_3[order(matched_geog_3$pair_id),]
matched_geog_3 <- matched_geog_3[!duplicated(matched_geog_3), ]
matched_geog_3$match <- 3

geog_postdem <- rbind(matched_geog_1, matched_geog_2, matched_geog_3)
setwd("~/Dropbox/switchers/Matching/data/matches/APR_R&R")
write.table(geog_postdem, "geog_postdem.csv", sep = ",", row.names = FALSE)